const {
  $util,
} = getApp();
Page({
  data: {
    currentIndex: 0,
    currentPageNum: 0,
    totalPageNum: 0,
    userAnswers: [],
    questionDetail: '',
    hiddenQueList: true,
    showList: [],
    imgUrl: $util.getImgUrl(),
    toView: 'inToView0',
  },
  btnPrevent: false,
  onLoad(opts) {
    wx.setNavigationBarTitle({
      title: opts.title || '考试中心',
    });
    this.setData(opts, () => {
      this.loadRecordDetail(opts.typ);
    });
  },
  loadRecordDetail(typ) {
    $util.request({
      url: 'entry/wxapp/edu.exam.testhistory',
      data: {
        reviewid: this.data.id,
        id: this.data.recordid,
      },
      success: (res) => {
        let plist = [];
        if (typ === 'rightdata') {
          plist = res.rightdata;
        } else if (typ === 'errdata') {
          plist = res.errdata;
        } else if (typ === 'all') {
          plist = res.result;
        }
        // 兼容题目序号
        let all = res.result;
        if (typ !== 'all') {
          for (let i = 0, lg = plist.length; i < lg; i++) {
            for (let j = 0, lgg = all.length; j < lgg; j++) {
              if (String(plist[i].id) === String(all[j].id)) {
                plist[i].index = Number(j) + 1;
              }
            }
          }
        }
        this.setData({
          grade: res.info.grade,
          currentQue: plist[0],
          currentIndex: 0,
          currentPageNum: 1,
          totalPageNum: plist.length,
          queList: plist, // 完整题目
          queTitle: res.testpaper.title,
        });
        wx.setStorageSync('qList', plist); // 本地缓存存储题目
      },
    });
  },
  getPrevQuestion() {
    const {
      currentIndex,
      currentPageNum,
    } = this.data;
    const qList = wx.getStorageSync('qList');
    this.setData({
      currentQue: qList[currentIndex - 1],
      currentPageNum: currentPageNum - 1,
      currentIndex: currentIndex - 1,
    });
  },
  getNextQuestion() {
    const {
      currentIndex,
      currentPageNum,
    } = this.data;
    const qList = wx.getStorageSync('qList');
    this.setData({
      currentQue: qList[currentIndex + 1],
      currentPageNum: currentPageNum + 1,
      currentIndex: currentIndex + 1,
    });

    const len = qList.length;
    if (currentPageNum === len - 1) {
      wx.showToast({
        title: '最后一题啦！',
        icon: 'none',
      });
    }
  },
  showQueList() {
    const qList = wx.getStorageSync('qList');
    let {
      hiddenQueList,
    } = this.data;
    const showList = [];
    for (const item of qList) {
      const tempObj = {
        title: item.title,
        state: item.user_answer,
        index: item.index,
      };
      showList.push(tempObj);
    }
    hiddenQueList = !hiddenQueList;
    this.setData({
      showList,
      hiddenQueList,
    });
  },
  showQuestion(e) {
    const { index } = e.currentTarget.dataset;
    const qList = wx.getStorageSync('qList');
    this.setData({
      currentQue: qList[index],
      currentIndex: index,
      currentPageNum: index + 1,
      hiddenQueList: true,
    });
  },
  scrollToViewFn(e) {
    this.setData({
      toView: 'inToView' + e.target.dataset.index,
      hiddenQueList: true,
    });
  },
});
